<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view v-if="address" class="address-detail">
        <view class="address-detail">{{ detailAddress }}</view>
        <view class="address-user"
          ><text>{{ address.userName }}</text>
          <text>{{ address.telNumber }}</text></view
        >
      </view>
      <view v-else class="address-btn u-flex u-row-center">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          :custom-style="{ backgroundColor: '#fff!important' }"
          @click="handleChooseAddress"
          >获取收货地址</u-button
        >
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view class="goods-list u-p-15">
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view
          class="cart-item"
          v-for="item in checkedCarts"
          :key="item.goods_id"
        >
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">X{{ item.num }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ checkedCartPrice }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handlePay"
          >去支付({{ checkedCartNum }})</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },
  methods: {
    ...mapActions("user", ["getToken"]),
    // 点击获取收货地址
    async handleChooseAddress() {
      const [err, res] = await uni.chooseAddress();
      // console.log(res);
      this.address = res;
    },
    // 点击 支付按钮
    async handlePay() {
      //判断有没有token 没有  调用action 来获取token - 设置到vuex中
      if (!this.token) {
        // console.log(this);
        // 获取token
        await this.getToken();
      }
      //#region 1  获取token
      //  获取用户的信息
      // const [err1, res] = await uni.getUserProfile({
      //   desc: "用来给用户提供更好的体验",
      // });
      // console.log(err1, res);
      //  调用 wx.login  来获取  code （临时登录凭据 5分钟）
      // const [err2, { code }] = await uni.login();
      // console.log(err2, code);
      //  调用后台 获取token
      // const result1 = await this.$u.post("/users/wxlogin", { ...res, code });
      // console.log(104, result1);
      // const { token } = result1.message;
      // console.log(106, token);
      // token写死
      // const token =
      //   "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjE3LCJpYXQiOjE2NjU5ODg1MzUsImV4cCI6MTAwMTY2NTk4ODUzNH0.VM4CBB8PHjgwBYeH4mWkJZ2AVwAnzdFlO2LRQO48XyU";

      //#endregion

      //#region 2 创建订单
      // 请求头参数要求
      // 请求体参数要求
      const orderParams = {
        // 订单总价格
        order_price: this.checkedCartPrice,
        // 收货地址
        consignee_addr: this.detailAddress,
        // 订单数组
        goods: this.checkedCarts.map((v) => ({
          goods_id: v.goods_id,
          goods_number: v.num,
          goods_price: v.goods_price,
        })),
      };
      // console.log(orderParams);
      // 2 调用创建订单接口
      const result2 = await this.$u.post("/my/orders/create", orderParams);
      // console.log(result2);

      //#endregion
      //#region 3 获取订单编号

      // 订单编号
      const { order_number } = result2.message;

      //#endregion

      //#region 4 获取支付参数
      //获取支付参数

      const result3 = await this.$u.post("/my/orders/req_unifiedorder", {
        order_number,
      });
      // console.log(result3);
      // 支付参数
      const { pay } = result3.message;
      // console.log(155, pay);
      //#endregion
      //#region 5 调起小程序内 微信支付
      //调起小程序内 微信支付
      const result4 = await uni.requestPayment(pay);
      console.log(result4);
      //#endregion
    },
  },
  computed: {
    // checkedCarts  选中的商品数组
    ...mapGetters("cart", [
      "checkedCarts",
      "checkedCartPrice",
      "checkedCartNum",
    ]),
    ...mapState("user", ["token"]),
    // 详细收货地址信息
    detailAddress() {
      if (!this.address) {
        return "";
      }
      const { cityName, countyName, detailInfo, provinceName } = this.address;
      return provinceName + cityName + countyName + detailInfo;
    },
  },
};
</script>

<style lang="scss">
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: #eb4450;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>